import qqmap from '../../utils/qqmap'

Page({
  data:{
    markers:[{
      id:1,
      content:'123',
      latitude: 30.59982, 
      longitude: 114.3096,
      width:24,
      height:30
     },{
      id:2,
      latitude: 32.90982, 
      longitude: 114.3096,
      width:24,
      height:30
     }],
     latitude: 30.59982, 
     longitude: 114.3096,
     polyline:[],
     scale:16,
  },
  butMap(){

    this.setData({scale:6})
    this.getPolyline()
  },
  // 地图划线
  getPolyline(){
    qqmap.direction({
      mode:'walking',
      from:'30.59982,114.3096',
      to:'32.90982,114.3096',
      success: (res) => {
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        this.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#0070d9',
            width: 5
          }]
        })
      }
    })
  }
})